package view.dialog;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import server.exception.LoginException;
import server.exception.RegistrationException;
import view.ClientGUI;

/**
 * Prompt user for registration/login.
 */
public class StartupDialog extends JDialog {

	private static final long serialVersionUID = 79867536580486824L;
	
	private JTextField username;
	private JPasswordField password;
	private ClientGUI cg;
	
	/**
	 * The only constructor
	 * @param cg
	 * 		The Frame of the application
	 */
	public StartupDialog(ClientGUI cg) {
		this.cg = cg;

		setTitle("Authenticate");
		setLayout(new GridBagLayout());
		setModal(true);

		username = new JTextField(15);
		password = new JPasswordField(15);

		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(5, 5, 5, 5);
		c.weightx = 0.5;
		c.gridy = 0;

		c.gridy++;
		add(new JLabel("username"), c);
		add(username, c);

		c.gridy++;
		add(new JLabel("password"), c);
		add(password, c);

		JPanel buttons = new JPanel();
		JButton create = new JButton("register");
		JButton login = new JButton("login");
		create.addActionListener(new RegisterListener());
		login.addActionListener(new LoginListener());
		buttons.add(create);
		buttons.add(login);

		c.gridy++;
		c.gridwidth = 2;
		add(buttons, c);

		pack();
		
		// centre and display
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/** Handles registering with user entered details & logs user in */
	class RegisterListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				// try to register the user
				cg.getServer().registerUser(cg.getClient(), username.getText(),
						new String(password.getPassword()));
				cg.getBuyPanel().getBuyButton().setEnabled(true);
				StartupDialog.this.dispose();
			} catch (RemoteException e) {
				JOptionPane.showMessageDialog(StartupDialog.this,
						"An error occured while communicating with server.",
						"Error", JOptionPane.ERROR_MESSAGE);
			} catch (RegistrationException e) {
				JOptionPane.showMessageDialog(StartupDialog.this,
						e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}
		}

	}

	/** Handles registering with user entered details & logs user in */
	class LoginListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				// try to login the user
				cg.getServer().loginUser(cg.getClient(), username.getText(),
						new String(password.getPassword()));
				cg.getBuyPanel().getBuyButton().setEnabled(true);
				StartupDialog.this.dispose();
			} catch (RemoteException e) {
				JOptionPane.showMessageDialog(StartupDialog.this,
						"An error occured while communicating with server: "
								+ e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			} catch (LoginException e) {
				JOptionPane.showMessageDialog(StartupDialog.this,
						e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}

		}

	}

}
